package com.goodsogood.union.auth.task;

import org.springframework.scheduling.annotation.Scheduled;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

//@Component
public class Task {
    private static boolean DEBUG = true;

    static {
        Enumeration allNetInterfaces;
        try {
            allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                Enumeration addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    ip = (InetAddress) addresses.nextElement();
                    if (ip != null && ip instanceof Inet4Address) {
                        if (ip.getHostAddress().equals("121.42.51.149")) {
                            DEBUG = false;
                            System.out.println("============ Debug Mode Off ==================");
                        }
                    }
                }
            }
        } catch (SocketException e) {
            DEBUG = true;
            e.printStackTrace();
        }
    }

    @Scheduled(cron = "0 0/20 * * * ?")
    public void checkEncloser() {
        if (DEBUG) {
            return;
        }
        long t1 = System.currentTimeMillis();
        System.out.println("============ TASK ON: checkEncloser ==================");
        System.out.println("============ TASK DONE: checkEncloser (" + (System.currentTimeMillis() - t1) + ")===========");
    }
}
